package com.gt.wxpay.cashier.common;

import cn.hutool.core.util.IdUtil;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * packageName com.gt.wxpay.cashier.payment.application
 *
 * @author GuoTong
 * @version JDK 8
 * @className RuleCommonUilt
 * @date 2024/3/16
 * @description 规则生成器
 */
public class RuleCommonUilt {

    /**
     * 分布式锁前缀
     */
    public final static String CLOSE_ORDER_LOCK_PREFIX = "payOrderClose_";

    public final static String PAY_ORDER_LOCK_PREFIX = "payOrder_";
    public final static String PAY_REFUND_LOCK_PREFIX = "payRefund_";


    /**
     * 支付订单流水号前缀
     */
    public final static String ORDER_NO_RULE_PREFIX = "P";

    /**
     * 退款订单流水浅醉
     */
    public final static String REFUND_NO_RULE_PREFIX = "R";

    /**
     * 微信支付渠道使用的ID
     */
    public final static String CHANNEL_CONFIG_SYSTEM = "wx.payment.channel.system";

    /**
     * 微信支付回调地址使用/wxpay/notify/pay
     */
    public final static String WX_CALL_BACK_URL_SYSTEM = "wx.payment.backUrl.system";


    // 微信支付下单的类型----begin----------------

    public final static String WX_JSAPI = "JSAPI";

    public final static String WX_NATIVE = "NATIVE";

    public final static String WX_APP = "APP";

    public final static String WX_MICROPAY = "MICROPAY";

    public final static String WX_MWEB = "MWEB";

    public final static String WX_FACEPAY = "FACEPAY";

    // 微信支付下单的类型----ending----------------

    public final static String NOT_SUPPER_TYPE = "暂不支持该支付方式：";
    public final static String OPERATION_FAIL_BY_PAY_ORDER_NOT_FOUND = "操作发起失败，找不到支付订单：";

    /**
     * 获取下一个雪花策略值
     *
     * @return String
     */
    public static String getOrderNoNextValue() {
        return ORDER_NO_RULE_PREFIX + IdUtil.getSnowflakeNextIdStr();
    }

    /**
     * 获取下一个雪花策略值
     *
     * @return String
     */
    public static String getRefundNoNextValue() {
        return REFUND_NO_RULE_PREFIX + IdUtil.getSnowflakeNextIdStr();
    }

    public static String extractIP(String url) {
        String ipPattern = "\\b(?:[0-9]{1,3}\\.){3}[0-9]{1,3}\\b";
        Pattern pattern = Pattern.compile(ipPattern);
        Matcher matcher = pattern.matcher(url);
        if (matcher.find()) {
            return matcher.group();
        }
        return null;
    }
}
